//
// Created by 周杰 on 2020/1/13.
//

#include "EdgeDetectTest.h"

const string image_path = "/Volumes/D/study/machinelearning/opencv/testpic/lenacolor.bmp";

/**
 * 比较看canny算子检测的边缘很清晰，sobel检测的边缘看着舒服些，比较平滑
 */
void EdgeDetectTest::edgeDetectTest() {
    Mat source_image = imread(image_path);
    imshow("source image.", source_image);

    //转灰度图片
    Mat gray_image;
    cvtColor(source_image, gray_image, ColorConversionCodes::COLOR_BGR2GRAY);
    imshow("gray image.", gray_image);

    //进行sobel算法进行边缘提取
    //先提取x方向的边缘
    Mat sobel_image_x_16s;
    Sobel(gray_image, sobel_image_x_16s, CV_16S, 1, 0);
    Mat sobel_x_8u;
    //将16位有符号单通道转换成8位无符号单通道
    convertScaleAbs(sobel_image_x_16s, sobel_x_8u);
    imshow("sobel x 8u.", sobel_x_8u);

    //直接测试提取x方向的,8位无符号单通道图片，提取的特征会少一些
//    Mat sobel_8u;
//    Sobel(gray_image,sobel_8u,CV_8U,1,0);
//    imshow("sobel 8u",sobel_8u);

    //提取y方向上的特征
    Mat sobel_y_16s;
    Sobel(gray_image, sobel_y_16s, CV_16S, 0, 1);
    Mat sobel_y_8u;
    convertScaleAbs(sobel_y_16s, sobel_y_8u);
    imshow("sobel y 8u", sobel_y_8u);

    //将x方向的特征和y方向的特征合并
    Mat sobel_result;
    addWeighted(sobel_x_8u, 0.5, sobel_y_8u, 0.5, 0, sobel_result);
    imshow("sobel result.", sobel_result);

    //进行canny算子边缘提取
    Mat canny_image;
    Canny(gray_image, canny_image, 30, 120);
    imshow("canny image.", canny_image);

    waitKey();
    destroyAllWindows();
}